package com.Interface.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Interface.dao.ClassInfoMapper;
import com.Interface.dao.ClassMemberAddMapper;
import com.Interface.dao.ClassMemberInfoMapper;
import com.Interface.dao.UserInfoMapper;
import com.Interface.entity.ClassMemberAdd;
import com.Interface.entity.ClassMemberInfo;

@Service
public class ClassMemberAddService {
	@Autowired
	ClassInfoMapper classInfoDao;
	@Autowired
	ClassMemberAddMapper classMemberAddDao;
	@Autowired
	ClassMemberInfoMapper classMemberDao;
	@Autowired
	UserInfoMapper userDao;
	public List<Map<String, Object>>  getAddMemberNotice(int user_id){
		return classMemberAddDao.getAddMemberNotice(user_id);
	}
	
	
	public String classMemberAddCheck(Map<String, Object> param){
		ClassMemberAdd classMemberAdd = classMemberAddDao.selectByPrimaryKey(Integer.parseInt(param.get("notice_id").toString()));
		int rlt;
		if(classMemberAdd==null){
			return "查无该消息";
		}
		if(classMemberAdd.getNoticeUserId()!=Integer.parseInt(param.get("user_id").toString())){
			return "您无权通过该条审核";
		}
		if(Integer.parseInt(param.get("type").toString())==1){
			
			 param.put("class_id", classMemberAdd.getClassId());
			 param.put("user_id", classMemberAdd.getUserId());
			 rlt=classMemberDao.isUserInClass(param);
			 if(rlt==1){
				 return  "用户已经加入班组";
			 }
			 ClassMemberInfo classMemberInfo = new ClassMemberInfo();
			 classMemberInfo.setClassId(Integer.parseInt(param.get("class_id").toString()));
			 classMemberInfo.setUserId(param.get("user_id").toString());
			 classMemberInfo.setIsSelf(1);
			 classMemberInfo.setJoinTime(new Date());
			 rlt= classMemberDao.insertSelective(classMemberInfo);
			 if(rlt==0){
				  return  "新增班组失败,插入班组成员失败";
			 }
			 rlt=classInfoDao.addClassMemberCnt(Integer.parseInt(param.get("class_id").toString()));
			 if(rlt==0){
				  return  "更新班组成员数量出错";
			 }
			 rlt=userDao.addClassCntByUserId(Integer.parseInt(param.get("user_id").toString()));
			 if(rlt==0){
				 return  "用户班组数量出错";
			 }
		}
		/*
		ClassMemberAdd classMemberAddTmp = new ClassMemberAdd();
		classMemberAddTmp.setId(classMemberAdd.getId());
		classMemberAddTmp.setStatus(Integer.parseInt(param.get("type").toString()));
		classMemberAddDao.updateByPrimaryKeySelective(classMemberAddTmp);
		*/
		Map<String,Object> hasParam = new HashMap<String,Object>();
		hasParam.put("status", Integer.parseInt(param.get("type").toString()));
		hasParam.put("class_id", classMemberAdd.getClassId());
		hasParam.put("user_id", classMemberAdd.getUserId());
		hasParam.put("notice_user_id", classMemberAdd.getNoticeUserId());
		classMemberAddDao.updateHasMemberAdd(hasParam);
		return "";
	}
}
